package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.PageResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("itemcat")
public class ItemCatController {

    Logger logger = LoggerFactory.getLogger(ItemCatController.class);
    @DubboReference
    private TbItemCatService      tbItemCatService;
    @DubboReference
    private TbTypeTemplateService tbTypeTemplateService;
    /*商品分类管理页面跳转*/
    @RequestMapping("runItemCat")
    public String runSeller(Model model){
        List<TbTypeTemplate> list = tbTypeTemplateService.selectAll();
        model.addAttribute("typeList",list);
        return "admin/item_cat";
    }
    /*商品分类管理页面展示*/
    @RequestMapping("selectInfoParentIdServer")
    @ResponseBody
    public PageResult<TbItemCat>
    selectInfoParentIdServer(TbItemCat itemCat, @RequestParam(value = "parentId",defaultValue = "0") Long parentId,
                                                          @RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                                                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        PageResult<TbItemCat> pageBean = tbItemCatService.selectInfoParentIdServer(itemCat,parentId,pageNumber,pageSize);
        return pageBean;
    }

    /*更具parentId查询*/
    @RequestMapping("selectByParentId/{parentId}")
    @ResponseBody
    public AjaxResult<TbItemCat> selectByParentId(@PathVariable("parentId") Long parentId){
        try {
            TbItemCat itemCat = tbItemCatService.selectByParentId(parentId);
            if (itemCat!=null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(itemCat);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("更具parentId查询失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*添加*/
    @RequestMapping("addItemcat")
    @ResponseBody
    public AjaxResult addItemcat(TbItemCat tbItemCat){
        try {
            int i = tbItemCatService.addItemcat(tbItemCat);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加商品分类失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    /*修改,回显*/
    @RequestMapping("getUpdateItemcatSelect/{id}")
    @ResponseBody
    public AjaxResult getSelectItemcatById(@PathVariable("id") Long id){
        try {
            List<TbTypeTemplate> list = tbTypeTemplateService.selectAll();
            TbItemCat tbItemCat = tbItemCatService.getSelectItemcatById(id);
            Map<String,Object> map = new HashMap<>();
            map.put("list",list);
            map.put("tbItemCat",tbItemCat);
            if (tbItemCat != null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(map);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("修改,回显商品分类失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }

    /*执行修改*/
    @RequestMapping("getUpdateItemcat")
    @ResponseBody
    public AjaxResult getUpdateItemcat(TbItemCat tbItemCat){
        try {
            int i = tbItemCatService.getUpdateItemcat(tbItemCat);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("删除商品分类失败失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }


}
